package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Coursefree;
import com.biostime.qdingding.modules.qd.service.CoursefreeService;
import com.biostime.qdingding.modules.sys.entity.User;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 课程免费体验Controller
 * @author king
 * @version 2015-12-24
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/coursefree")
public class CoursefreeController extends BaseController {

	@Autowired
	private CoursefreeService coursefreeService;
	
	@ModelAttribute
	public Coursefree get(@RequestParam(required=false) String id) {
		Coursefree entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = coursefreeService.get(id);
		}
		if (entity == null){
			entity = new Coursefree();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:coursefree:view")
	@RequestMapping(value = {"list", ""})
	public String list(Coursefree coursefree, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Coursefree> page = coursefreeService.findPage(new Page<Coursefree>(request, response), coursefree); 
		model.addAttribute("page", page);
		return "modules/qd/coursefreeList";
	}

	//@RequiresPermissions("qd:coursefree:view")
	@RequestMapping(value = "dialog")
	public String dialog(Coursefree coursefree, Model model) {
		
		model.addAttribute("coursefree", coursefree);
		return "modules/qd/coursefreeDialog";
	}

	//@RequiresPermissions("qd:coursefree:edit")
	@RequestMapping(value = "saveDialog")
	public String saveDialog(Coursefree coursefree, Model model, RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, coursefree)){
			return dialog(coursefree, model);
		}
		
		/**设置中心**/
		User user = UserUtils.getUser();
		coursefree.setCenterId(StringUtils.toInteger(user.getCenter().getId()));
		
		coursefreeService.save(coursefree);
		
		model.addAttribute("script","<script type=\"text/javascript\">window.onload=function(){save();}</script>");
		
		return dialog(coursefree,model);
	}
	
	@RequiresPermissions("qd:coursefree:view")
	@RequestMapping(value = "form")
	public String form(Coursefree coursefree, Model model) {
		
		model.addAttribute("coursefree", coursefree);
		return "modules/qd/coursefreeForm";
	}

	@RequiresPermissions(value={"qd:coursefree:edit","qd:coursefree:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Coursefree coursefree, Model model, RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, coursefree)){
			return form(coursefree, model);
		}
		
		/**设置中心**/
		User user = UserUtils.getUser();
		coursefree.setCenterId(StringUtils.toInteger(user.getCenter().getId()));
		
		coursefreeService.save(coursefree);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/coursefree/?repage";
	}
	
	@RequiresPermissions("qd:coursefree:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Coursefree coursefree, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				coursefreeService.delete(new Coursefree(id));
			}
		}else{
			coursefreeService.delete(coursefree);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/coursefree/?repage";
	}

}